package user

import (
	"wx-uniapp-go/internal/domain/errcode"
	"wx-uniapp-go/internal/logic/points_logic"
	"wx-uniapp-go/internal/repository/dto"
	"wx-uniapp-go/internal/server/echo"

	"github.com/gin-gonic/gin"
)

// CreatePointsRule 创建积分规则
func CreatePointsRule(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	req := new(dto.CreatePointsRuleReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := points_logic.NewPointsLogic(ctx).CreatePointsRule(req); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// UpdatePointsRule 更新积分规则
func UpdatePointsRule(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	req := new(dto.UpdatePointsRuleReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := points_logic.NewPointsLogic(ctx).UpdatePointsRule(req); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// DeletePointsRule 删除积分规则
func DeletePointsRule(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	id := c.GetInt64("id")
	if id <= 0 {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := points_logic.NewPointsLogic(ctx).DeletePointsRule(id); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// ListPointsRules 查询积分规则列表
func ListPointsRules(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	req := new(dto.ListPointsRuleReq)
	if err := c.ShouldBindQuery(req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	resp, err := points_logic.NewPointsLogic(ctx).ListPointsRules(req)
	if err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c, resp)
}
